package org.sword.wechat4j.api;

import com.alibaba.fastjson.JSON;
import org.junit.Test;
import org.sword.wechat4j.TokenGet$jsonGet;
import org.sword.wechat4j.api.bean.user.UserTag;
import org.sword.wechat4j.api.request.user.tag.Cgi_binTagsGetGet;
import org.sword.wechat4j.api.response.ApiResponse;
import org.sword.wechat4j.api.response.user.UserTagList;
import org.sword.wechat4j.type.ErrorCode;

import java.io.IOException;
import java.util.List;

/**
 * Created by wxc on 2016/9/10.
 */
public class ApiUtil {

    private String access_token;

    /**
     * access_token
     * @return
     * @throws IOException
     */
    public static String getCode() throws IOException {
        return new WechatClient("http://wxpublish.571xz.net", null).execute(new TokenGet$jsonGet()).getAccess_token();
    }

    @Test
    public void template() throws IOException {
        WechatClient client = new WechatClient();

        //获取所有用户标签
        Cgi_binTagsGetGet tagsGet = new Cgi_binTagsGetGet();
        UserTagList list = client.execute(tagsGet, access_token);
        if (list.isSuccess()) {
            List<UserTag> tags = list.getTags();
            for (UserTag tag : tags) {
                System.out.println(tag.getName());
            }
        }
    }

    @Test
    public void test() throws IOException {
        System.out.println(getCode());
    }

    public static void show(ApiResponse response) {
        if (response.isSuccess())
            System.out.println(JSON.toJSONString(response, true));
        else
            System.out.println(ErrorCode.mean(response.getErrcode()));
    }
}
